﻿using SpeakTechnology.LogicOfBussiness;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SpeakTechnology.Presentation
{
    public partial class SEARCHPRODUCT : Form
    {
        public SEARCHPRODUCT()
        {
            InitializeComponent();
        }

        private void Clear()
        {
            BoxName.Clear();
            BoxQuantity.Clear();
            BoxCategory.Clear();
            BoxPrice.Clear();
            boxbrand.Clear();
            BoxName.Enabled = true;
            BoxCategory.Enabled = false;
            BoxQuantity.Enabled = false;
            BoxPrice.Enabled = false;
            boxbrand.Enabled = false;
        }

        private void CLOSE_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SEARCH_Click(object sender, EventArgs e)
        {
            Validation va = new Validation();
            if (va.Validar_Name(BoxName.Text))
            {
                MessageBox.Show(" What Have you  ingress the name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                bool respuesta = C_Product.SEARCHP(BoxName.Text,boxbrand.Text);

                if (respuesta)
                {
                    var user = C_Product.Query(BoxName.Text);
                    BoxName.Text = user.Nombre;
                    BoxQuantity.Text = Convert.ToString(user.Cantidad);
                    BoxCategory.Text = user.Categoria.Categoria1;
                    BoxPrice.Text = Convert.ToString(user.Precio);
                    boxbrand.Text = user.Marca.Marca1;
                    BoxName.Enabled = false;
                    BoxQuantity.Enabled = true;
                    BoxPrice.Enabled = true;
                    MessageBox.Show("Name of product exist", "correct", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Name  of product not exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void UPDATE_Click(object sender, EventArgs e)
        {
            Validation va = new Validation();
            if (va.Validar_Name_Quantity_Price(BoxName.Text, BoxQuantity.Text, BoxPrice.Text))
            {
                MessageBox.Show(" What Have you  ingress  All the Box Text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                C_Product C_P = new C_Product();
                if (C_P.UPDATE(BoxName.Text, BoxQuantity.Text, BoxCategory.Text, BoxPrice.Text))
                {
                    MessageBox.Show("It´s updated the product", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Clear();
                   
                }
            }

        }
    
        private void BoxQuantity_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void BoxPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void DELETE_Click(object sender, EventArgs e)
        {
            Validation va = new Validation();
            if (va.Validar_Name(BoxName.Text))
            {
                MessageBox.Show("What have you ingress a name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                C_Product C_P = new C_Product();
                if (C_P.DELETE(BoxName.Text, BoxQuantity.Text, BoxCategory.Text, BoxPrice.Text,boxbrand.Text))
                {
                    MessageBox.Show("It´s deleted the product", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Clear();
                   
                    
                }
            }
        }

        private void BoxName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

    }
}
